package com.huawei.hwCloudJs.service.jsapi;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.webkit.WebView;
import android.widget.Toast;
import com.huawei.cloudservice.CloudAccountManager;
import com.huawei.fastengine.fastview.startFastappEngine.bean.RpkInfo;
import com.huawei.hms.framework.network.restclient.dnkeeper.DNKeeperConfig;
import com.huawei.hms.support.api.entity.pay.HwPayConstant;
import com.huawei.hwCloudJs.annotation.JSClassAttributes;
import com.huawei.hwCloudJs.annotation.JSMethod;
import com.huawei.hwCloudJs.core.JSRequest;
import com.huawei.hwCloudJs.core.JsCallback;
import com.huawei.hwCloudJs.core.a.b;
import com.huawei.hwCloudJs.d.a;
import com.huawei.hwCloudJs.d.d;
import com.huawei.hwCloudJs.d.e;
import com.huawei.hwCloudJs.d.f;
import com.huawei.hwCloudJs.support.a.c;
import com.huawei.hwid.core.constants.HwAccountConstants;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;

@JSClassAttributes
public class JsCoreApi extends JSRequest {
    private static final String a = "JsCoreApi";

    @JSMethod(isOpen = true)
    public void checkAvailability(CheckAvailReq checkAvailReq, JsCallback jsCallback) {
        Log.i(a, "checkAvailability done");
        if (checkAvailReq == null || checkAvailReq.getApiList() == null) {
            jsCallback.failure();
            return;
        }
        JSONObject jSONObject = new JSONObject();
        try {
            List apiList = checkAvailReq.getApiList();
            int size = apiList.size();
            for (int i = 0; i < size; i++) {
                String str = (String) apiList.get(i);
                jSONObject.put(str, b.a().b(str) != null);
            }
            jsCallback.success(jSONObject.toString());
        } catch (JSONException e) {
            Log.e(a, "JS checkAvailability JSONException");
            jsCallback.failure();
        }
    }

    @JSMethod(isOpen = true, name = "close", permission = "com.huawei.javascript.base")
    public void close(JsCallback jsCallback) {
        Log.i(a, "JS close Webview ");
        WebView webView = jsCallback.getWebView();
        if (webView == null) {
            Log.e(a, "JS close Webview null");
            jsCallback.failure();
            return;
        }
        Context context = webView.getContext();
        if (context instanceof Activity) {
            ((Activity) context).finish();
        } else {
            jsCallback.failure();
        }
    }

    @JSMethod(isOpen = true, name = "getAppInfo", permission = "com.huawei.javascript.base")
    public void getAppInfo(String str, JsCallback jsCallback) {
        Log.i(a, "JS getAppInfo");
        WebView webView = jsCallback.getWebView();
        if (webView == null) {
            jsCallback.failure();
            return;
        }
        Context context = webView.getContext();
        if (f.c(context, str)) {
            String a = a.a().a(context, str);
            String a2 = f.a(context, str);
            int b = f.b(context, str);
            try {
                JSONObject jSONObject = new JSONObject();
                jSONObject.put(HwPayConstant.KEY_SIGN, a);
                jSONObject.put(CloudAccountManager.KEY_VERSION_NAME, a2);
                jSONObject.put(RpkInfo.VERSIONCODE, b + "");
                jsCallback.success(jSONObject.toString());
                return;
            } catch (JSONException e) {
                Log.e(a, "JS getAppInfo JSONException!");
                jsCallback.failure();
                return;
            }
        }
        Log.e(a, "JS getAppInfo package not Installed!");
        jsCallback.failure(11);
    }

    @JSMethod(isOpen = true)
    public void getBrowserInfo(JsCallback jsCallback) {
        Log.i(a, "JS getBrowserInfo");
        WebView webView = jsCallback.getWebView();
        if (webView == null) {
            jsCallback.failure();
            return;
        }
        jsCallback.success(a.a().a(webView.getContext()));
    }

    @JSMethod(isOpen = true, name = "getNetworkType", permission = "com.huawei.javascript.base")
    public void getNetworkType(JsCallback jsCallback) {
        Log.i(a, "JS  getNetworkType");
        String b = d.b();
        JSONObject jSONObject = new JSONObject();
        try {
            jSONObject.put("networkType", b);
            jsCallback.success(jSONObject.toString());
        } catch (JSONException e) {
            jsCallback.failure();
        }
    }

    @JSMethod(isOpen = true, name = "getSystemInfo", permission = "com.huawei.javascript.base")
    public void getSystemInfo(JsCallback jsCallback) {
        try {
            JSONObject jSONObject = new JSONObject();
            jSONObject.put("model", a.c());
            jSONObject.put("romVersion", a.d());
            jSONObject.put("osVersion", a.b());
            jSONObject.put("region", a.f());
            jSONObject.put("language", a.e());
            jsCallback.success(jSONObject.toString());
        } catch (JSONException e) {
            jsCallback.failure();
        }
    }

    @JSMethod(isOpen = true, name = "postChannelMessage", permission = "com.huawei.javascript.base")
    public void postChannelMessage(ChannelMessageReq channelMessageReq, JsCallback jsCallback) {
        Log.i(a, "JS  postChannelMessage");
        if (channelMessageReq == null) {
            Log.e(a, "JS postChannelMessage JS_CONFIG_RET_CODE_PARSE_PARAM_ERROR");
            jsCallback.failure(13);
            return;
        }
        WebView webView = jsCallback.getWebView();
        if (webView == null) {
            jsCallback.failure();
            return;
        }
        String a = a.a(webView);
        String str = jsCallback.getwebviewId();
        String str2 = null;
        try {
            URL url = new URL(a);
            str2 = url.getProtocol() + "://" + url.getAuthority();
        } catch (MalformedURLException e) {
            Log.e(a, "JS postChannelMessage MalformedURLException");
            jsCallback.failure();
        }
        channelMessageReq.setOrigin(str2);
        channelMessageReq.setSource(str);
        com.huawei.hwCloudJs.service.jsmsg.a.a().a((com.huawei.hwCloudJs.support.b.d) channelMessageReq);
    }

    @JSMethod(isOpen = true, name = "preload", permission = "com.huawei.javascript.base")
    public void preload(PreloadReq preloadReq, JsCallback jsCallback) {
        Log.i(a, "JS  preload");
        if (preloadReq == null || preloadReq.getUrlList() == null) {
            Log.e(a, "JS  preload JS_RET_CODE_PARSE_PARAM_ERROR");
            jsCallback.failure(13);
            return;
        }
        WebView webView = jsCallback.getWebView();
        if (webView == null || webView.getContext() == null) {
            jsCallback.failure();
        } else {
            c.a((String[]) preloadReq.getUrlList().toArray(new String[0]), preloadReq.getFlag(), webView.getContext());
        }
    }

    @JSMethod(isOpen = true, name = "share", permission = "com.huawei.javascript.base")
    public void share(ShareReq shareReq, JsCallback jsCallback) {
        Log.i(a, "JS share");
        WebView webView = jsCallback.getWebView();
        if (webView == null) {
            jsCallback.failure();
        } else if (shareReq == null) {
            jsCallback.failure(13);
        } else {
            String title = shareReq.getTitle();
            String desc = shareReq.getDesc();
            String link = shareReq.getLink();
            if (desc == null) {
                desc = "";
            }
            if (link == null) {
                link = "";
            }
            Intent intent = new Intent();
            intent.setAction("android.intent.action.SEND");
            intent.putExtra("android.intent.extra.TEXT", title + ":" + desc + HwAccountConstants.BLANK + link);
            intent.setType(DNKeeperConfig.DNKEEPER_CONTENT_TYPE);
            try {
                webView.getContext().startActivity(Intent.createChooser(intent, webView.getContext().getString(e.b(webView.getContext(), "jssdk_share"))));
            } catch (ActivityNotFoundException e) {
                Log.e(a, "JS share ActivityNotFoundException");
                jsCallback.failure();
            }
        }
    }

    @JSMethod(isOpen = true, name = "showDialog", permission = "com.huawei.javascript.base")
    public void showDialog(DialogReq dialogReq, JsCallback jsCallback) {
        if (dialogReq == null) {
            jsCallback.failure(13);
            return;
        }
        WebView webView = jsCallback.getWebView();
        if (webView == null || webView.getContext() == null) {
            jsCallback.failure();
            return;
        }
        List buttons = dialogReq.getButtons();
        if (buttons == null || buttons.size() < 1 || buttons.size() > 2) {
            jsCallback.failure(13);
            return;
        }
        Builder builder = new Builder(webView.getContext());
        builder.setTitle(dialogReq.getTitle());
        builder.setMessage(dialogReq.getMessage());
        AlertDialog create = builder.create();
        create.setCanceledOnTouchOutside(false);
        create.show();
    }

    @JSMethod(isOpen = true, name = "showToast", permission = "com.huawei.javascript.base")
    public void showToast(ToastReq toastReq, JsCallback jsCallback) {
        int i = 1;
        Log.i(a, "JS  showToast");
        if (toastReq == null) {
            Log.e(a, "JS  showToast JS_RET_CODE_PARSE_PARAM_ERROR");
            jsCallback.failure(13);
            return;
        }
        WebView webView = jsCallback.getWebView();
        if (webView == null || webView.getContext() == null) {
            jsCallback.failure();
            return;
        }
        CharSequence title = toastReq.getTitle();
        if (toastReq.getDuration() != 1) {
            i = 0;
        }
        Toast.makeText(webView.getContext(), title, i).show();
    }
}
